package ca.camosun.proximity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Person implements Comparable<Person>, Iterable<SavedPerson> {
    private String firstName;
    private String lastName;
    private List<SavedPerson> myDevices;
     
    public Person(String inFirst, String inLast) {
            firstName = inFirst;
            lastName = inLast;
            myDevices = new ArrayList<SavedPerson>();
    }
    
    public String getFirstName() {
            return firstName;
    }
    
    public String getLastName() {
            return lastName;
    }
    public List<SavedPerson> addMyDevice(SavedPerson newDevice) {
    	myDevices.add(newDevice);
    	return myDevices;
    }
    
    public List<SavedPerson> getMyDevices() {
    	return myDevices;
    }
    
    public SavedPerson getDeviceByIndex(int index){
		 SavedPerson theDevice = myDevices.get(index); 
		
		return theDevice;
	}

	public void removeDevice(SavedPerson aDevice) {
		myDevices.remove(aDevice);
	}
	
	public int deviceCount() {
		return myDevices.size();
	}
	
	public int getDevicePosition(SavedPerson aDevice) {
		return myDevices.indexOf(aDevice);
	}

	@Override
	public Iterator<SavedPerson> iterator() {
		Collections.sort(myDevices);
		
		return myDevices.iterator();
	}
	
	public Iterator<SavedPerson> iteratorByName() {
		Collections.sort(myDevices, SavedPerson.byDeviceName());
		
		return myDevices.iterator();
	}
	
	
    @Override
    public boolean equals(Object what) {
            Person other = (Person) what;
            
            if( ! lastName.equals(other.lastName)) {
                    return false;
            }
            
            if( ! firstName.equals(other.firstName)) {
                    return false;
            }
            
            return true;
    }
    
    @Override
    public int compareTo(Person other) {
            int difference = lastName.compareTo(other.lastName);
            
            if( difference != 0) {
                    return difference;
            }
            
            difference = firstName.compareTo(other.firstName);
            
            return difference;
    }
}
